package com.ittsang.controller;

import com.ittsang.dto.LoginFormDTO;
import com.ittsang.dto.PageDTO;
import com.ittsang.dto.R;
import com.ittsang.dto.RoleUserDTO;
import com.ittsang.mapper.PostsMapper;
import com.ittsang.pojo.User;
import com.ittsang.service.ClubService;
import com.ittsang.service.RaceService;
import com.ittsang.service.UserClubPostsService;
import com.ittsang.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Resource
    UserService userService;

    @Resource
    ClubService clubService;

    @Resource
    RaceService raceService;

    @Resource
    UserClubPostsService userClubPostsService;



    @GetMapping("list")
    public R getUserList() {
        List<User> userList = userService.getUserList();
        return R.ok().put("list", userList);
    }

    @PostMapping("/managerLogin")
    public R managerLogin(@RequestBody LoginFormDTO loginForm) {
        return userService.managerLogin(loginForm);
    }

    /**
     * 发送手机验证码
     */
    @GetMapping("/code")
    public R sendCode(@RequestParam("telephone") String phone, HttpSession session) {
        // 发送短信验证码并保存
        return userService.sendCode(phone, session);
    }

    /**
     *  注册
     * @param loginForm
     * @param session
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody LoginFormDTO loginForm, HttpSession session) {
        // 单纯手机密码注册，后续补充信息
        return userService.register(loginForm, session);
    }

    /**
     *  密码登录
     * @param loginForm
     * @param session
     * @return
     */
    @PostMapping("/passwordLogin")
    public R passwordLogin(@RequestBody LoginFormDTO loginForm, HttpSession session) {
        // 密码登录
        return userService.passwordLogin(loginForm, session);
    }

    /**
     *  验证码登录
     * @param loginForm
     * @param session
     * @return
     */
    @PostMapping("/codeLogin")
    @CrossOrigin
    public R codeLogin(@RequestBody LoginFormDTO loginForm, HttpSession session) {
        // 验证码登录
        return userService.codeLogin(loginForm, session);
    }

    /**
     *  获取当前用户信息
     * @return
     */
    @GetMapping("/me")
    public R getUserInfo() {
        return userService.getUserInfo();
    }

    /**
     *  修改当前用户信息
     * @param user
     * @return
     */
    @PostMapping("/alterUserInfo")
    public R alterUserInfo(@RequestBody User user) {
        return userService.alterUserInfo(user);
    }

    /**
     *  修改手机号
     * @param phone
     * @return
     */
    @GetMapping("/alterUserPhone")
    public R alterUserPhone(@RequestParam("phone") String phone, HttpServletRequest request) {
        return userService.alterUserPhone(phone, request);
    }

    /**
     *  修改密码
     * @param pwd
     * @param npwd
     * @return
     */
    @GetMapping("/alterUserPassword")
    public R alterUserPassword(@RequestParam("pwd") String pwd, @RequestParam("npwd") String npwd, HttpServletRequest request) {
        return userService.alterUserPassword(pwd, npwd, request);
    }

    /**
     *  登出
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public R logout(HttpServletRequest request) {
        return userService.logout(request);
    }

    /**
     *  获取用户社团信息
     * @return
     */
    @GetMapping("/getUserClub")
    public R getUserClub() {
        return clubService.getUserClub();
    }

    /**
     *  获取社团人员列表,只有部长或者会长有此权限
     * @param pageDTO
     * @return
     */
    @PostMapping("/getClubUserList")
    public R getClubUserList(@RequestBody PageDTO pageDTO) {
        return clubService.getClubUserList(pageDTO);
    }

    /**
     *  获取社团全部部门
     * @return
     */
    @GetMapping("/getCLubDep")
    public R getCLubDep(@RequestParam("clubId") Integer clubId) {
        return clubService.getCLubDep(clubId);
    }

    /**
     *  获取社团某部门的成员信息
     * @param pageDTO
     * @return
     */
    @PostMapping("/getClubDepUserList")
    public R getClubDepUserList(PageDTO pageDTO) {
        // select * from user where cub_id = #{clubId} and department_id = #{departmentId} and is_deleted = 0;
        return clubService.getClubDepUserList(pageDTO);
    }

    /**
     *  获取用户已参加比赛数据
     * @param pageDTO
     * @return
     */
    @PostMapping("/getUserRace")
    public R getUserRace(@RequestBody PageDTO pageDTO) {
        return raceService.getUserRace(pageDTO);
    }

    /**
     *
     *  将某个成员的职位变化
     * @param
     * @return
     */
    @PostMapping("/insertRoleUser")
    public R insertRoleUser(@RequestBody RoleUserDTO roleUserDTO) {
        // 切记一定要获取社团职位，0代表是会长
        return userClubPostsService.insertRoleUser(roleUserDTO);
    }

    /**
     *  获取全部职位信息
     * @return
     */
    @GetMapping("/getAllPosts")
    public R getAllPosts() {
        return userClubPostsService.getAllPosts();
    }


    /**
     *  删除用户职位信息
     * @param roleUserDTO
     * @return
     */
//    @PostMapping("/deletedRoleUser")
    public R deletedRoleUser(RoleUserDTO roleUserDTO) {
        return userClubPostsService.deletedRoleUser(roleUserDTO);
    }

    /**
     *  用户退出社团，如果是最后一名会长，则无法退出社团，需要将会长职位交给别人
     * @return
     */
    @GetMapping("/logoutClub")
    public R logoutClub() {
        return userService.logoutClub();
    }
}
